import axios from 'axios';
import qs from 'qs';
import ServerMixin from '@/mixin/ServerMixin'
import {
    getUserInfo,
} from '@/utils/auth'
import utils from '@/utils'

export const doExec = (url, params, contentType, methods, headers) => {
    let pstr = qs.stringify(params, {
        arrayFormat: 'brackets'
    });
    return axios({
            url: (methods && methods.toUpperCase()) !== 'GET' ? url : `${url}?${pstr}`,
            params: null,
            data: (contentType && contentType.toUpperCase()) === 'JSON' ? params : ((contentType && contentType.toLowerCase()) === 'formdata' ? params : qs.stringify(params)),
            method: methods || 'POST',
            headers: {
                'x-auth-token': getUserInfo()
                    /*,'Content-Type': (headers && headers.toUpperCase()) === 'form' ? 'multipart/form-data' : 'application/x-www-form-urlencoded'*/
            }
        })
        .then((res) => {
            if (res.data.code == -2) {
                ServerMixin.Event.$emit('login', res.data.data);
                return {};
            } else {
                return res.data;
            }
        });
};